package com.nicefact.mailmonitor.dao;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.stereotype.Component;

import com.nicefact.mailmonitor.entity.AppUser;
import com.nicefact.mailmonitor.entity.BlobHistory;

@Component
public class BlobHistoryDao extends AbstractDao<BlobHistory>{
	
	public List<BlobHistory> getAll(){
		List<BlobHistory> objList = super.getAllObjects(BlobHistory.class);
		return objList;
	}
	
	public List<BlobHistory> getLastTenUploads(String createdBy){
		List<BlobHistory> objList = new ArrayList<BlobHistory>();
		PersistenceManager pm = super.getPersistenceManager();		
		Query query = pm.newQuery(BlobHistory.class);
		query.setOrdering("createDate desc");
		query.setFilter("createdBy==email");
		query.declareParameters("String email");
		query.setRange(0, 10);
		try {
			List<BlobHistory> results = (List<BlobHistory>) query.execute(createdBy);
			if (!results.isEmpty()) {
				objList.addAll(pm.detachCopyAll(results));
			}
		} finally {
			query.closeAll();
			pm.close();
		}
		return objList;
	}

}
